package org.zn.quality.handler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.tuple.Pair;
import org.zn.etl.misc.CompareUtils;
import org.zn.quality.entity.FaultsData;

public class CompareDataUtils {
	
	public static List<FaultsData> compareData(Map<Object, Map<String, Object>> mainData,
			Map<Object, Map<String, Object>> minordata, Map<String, String> columnMapping) {

		List<FaultsData> fdList = new ArrayList<>();

		for (Entry<Object, Map<String, Object>> ent : minordata.entrySet()) {
			// 一条
			Map<String, Object> mrow = mainData.get(ent.getKey());

			if (mrow != null) {

				Map<String, Object> miRow = ent.getValue();

				boolean dataEqual = true;

				List<Pair<Map<String, Object>, Map<String, Object>>> errorCols = new ArrayList<>();

				// 对比列 来自映射
				for (Entry<String, String> cent : columnMapping.entrySet()) {

					boolean colEqual = CompareUtils.valueEquals (mrow.get(cent.getKey().toLowerCase()), miRow.get(cent.getValue().toLowerCase()));

					if (!colEqual) {

						Map<String, Object> em = new HashMap<>();
						em.put(cent.getKey(), mrow.get(cent.getKey().toLowerCase()));

						Map<String, Object> emi = new HashMap<>();
						emi.put(cent.getKey(), miRow.get(cent.getValue().toLowerCase()));

						errorCols.add(Pair.of(em, emi));

					}

					dataEqual = dataEqual && colEqual;

				}
				
				if (!dataEqual) {
					
					FaultsData fd = new FaultsData();
					fd.setColumnList(errorCols);
					fd.setPk(ent.getKey());
					fdList.add(fd);
					fd.setType("column");	
				}

			}
			else {
				FaultsData fd = new FaultsData();
				fd.setPk(ent.getKey());
				fd.setType("miss");
				fdList.add(fd);
			}

		}

		return fdList;

	}
}
